package com.cn.view;


import com.cn.controller.Select;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GoodsManagement extends JInternalFrame {

    Select select = new Select();

    private JTextField textField;

    Object[] header= {"商品编号","商品名称","商品单价","商品库存","商品图片"};
    String sql = "SELECT goodsID,goodsName,goodsPrice,goodsCount,goodsImg FROM goods_goods";
    Object[][] data= select.getGoods(sql);
    DefaultTableModel df = new DefaultTableModel(data, header);
    int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

    public GoodsManagement() {
        super("商品管理系统");
        this.setBounds(0, 0, 700, 450);
        this.setIconifiable(true);
        this.setClosable(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击窗口的关闭按钮时程序执行的操作
        getContentPane().setLayout(null);

        JLabel label = new JLabel("请输入商品名称：");
        label.setBounds(151, 39, 112, 32);
        getContentPane().add(label);

        textField = new JTextField();
        textField.setBounds(263, 43, 127, 26);
        getContentPane().add(textField);
        textField.setColumns(10);

        JButton button = new JButton("查询");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String sql = "SELECT goodsID,goodsName,goodsPrice,goodsCount,goodsImg FROM goods_goods WHERE goodsName LIKE '%"+textField.getText()+"%'";
                Object[][] data = Select.getGoods(sql);
                df.setDataVector(data, header);
            }
        });
        button.setBounds(411, 40, 90, 30);
        getContentPane().add(button);

        JButton button_1 = new JButton("添加");
        button_1.setBounds(559, 140, 90, 30);
        getContentPane().add(button_1);
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GoodsManage t = new GoodsManage();
                t.setVisible(true);
                //关闭当前界面
                //dispose();
            }
        });

        JTable jTable = new JTable(df);
        JScrollPane jsp=new JScrollPane(jTable,v,h);
        jsp.setBounds(44, 103, 480, 282);
        getContentPane().add(jsp);

    }
    public static void main(String[] args) {
        GoodsManagement t = new GoodsManagement();
        t.setVisible(true);
    }

}